package com.molichuxing.gateway.bff.order.convertor;

import com.molichuxing.framework.dto.UploadFileRequestDto;
import com.molichuxing.framework.property.OperatorTypeEnum;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.framework.utils.NumberToCNUtils;
import com.molichuxing.services.business.dto.request.create.ContractRenegeBizCreateDto;
import com.molichuxing.services.business.dto.request.modify.OrderPayModifyBizDto;
import com.molichuxing.services.business.dto.response.NotifyBizDto;
import com.molichuxing.services.business.dto.response.UserBizDto;
import com.molichuxing.services.infrastructure.dto.request.create.InvoiceCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.OrderPayModifyDto;
import com.molichuxing.services.infrastructure.dto.response.*;
import com.molichuxing.services.property.*;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

public class Convert {

	public static OrderPayModifyBizDto toOrderPayModifyBizDto(NotifyBizDto notifyBizDto, UserDto userDto) {
		OrderPayModifyBizDto modifyDto = new OrderPayModifyBizDto();
		modifyDto.setOperateId(userDto.getId());
		modifyDto.setOperateName(userDto.getRealName());
		modifyDto.setOperatorType(OperatorTypeEnum.MEMBER);
		modifyDto.setOrderCode(notifyBizDto.getOrderCode());
		modifyDto.setPayTime(notifyBizDto.getPayTime());
		modifyDto.setPayWay(Enum.valueOf(OrderPayWayEnum.class, notifyBizDto.getPayWay().toString()));

		return modifyDto;
	}

	public static OrderPayModifyDto toOrderPayModifyDto(NotifyBizDto notifyBizDto, UserDto userDto) {
		OrderPayModifyDto modifyDto = new OrderPayModifyDto();
		modifyDto.setOperateId(userDto.getId());
		modifyDto.setOperateName(userDto.getRealName());
		modifyDto.setOperatorType(OperatorTypeEnum.MEMBER);
		modifyDto.setOrderCode(notifyBizDto.getOrderCode());
		modifyDto.setPayTime(notifyBizDto.getPayTime());
		modifyDto.setPayWay(Enum.valueOf(OrderPayWayEnum.class, notifyBizDto.getPayWay().toString()));
		modifyDto.setScheduleId(notifyBizDto.getScheduleId());

		return modifyDto;
	}

	public static InvoiceCreateDto toInvoiceCreateDto(NotifyBizDto notify, OrderExclusivelDto orderExclusivel,
			OrderScheduleDto orderSchedule, DealerDetailDto dealerDetail) {
		InvoiceCreateDto createDto = new InvoiceCreateDto();
		// 服务费
		BigDecimal serviceFee = orderExclusivel.getServiceFee().divide(new BigDecimal(orderSchedule.getTotalPeriods()),
				2, RoundingMode.HALF_DOWN);
		if (orderSchedule.getTotalPeriods() > 1 && orderSchedule.getTotalPeriods() == orderSchedule.getPeriods()) {
			// 最后一期
			serviceFee = orderExclusivel.getServiceFee()
					.subtract(serviceFee.multiply(new BigDecimal(orderSchedule.getTotalPeriods() - 1)));
		}

		// 订单号
		createDto.setOrderCode(orderExclusivel.getOrderCode());
		// 下单时间
		createDto.setOrderTime(orderExclusivel.getOrderTime());
		// 支付时间/续订单生效的时
		createDto.setPayTime(notify.getPayTime());
		// 支付金额
		createDto.setAmount(orderSchedule.getPayable());
		// 服务费
		createDto.setServiceFee(serviceFee);
		// 业务类型（1：订阅里程，2：专属里程）
		createDto.setBusinessType(InvoiceBusinessTypeEnum.EXCLUSIVEL.getValue());
		// 发票类型（11：主订单，12：续订单，13：加购，14：超时，15：超程，16：质损,21：分期，22：全款）
		createDto.setType(OrderPaymentMethodEnum.INSTALLMENT.equals(orderExclusivel.getPaymentMethod())
				? InvoiceTypeEnum.INSTALLMENT.getValue() : InvoiceTypeEnum.FULL_PAYMENT.getValue());
		// 当前期数
		createDto.setCurrentPeriods(orderSchedule == null ? 1 : orderSchedule.getPeriods());
		// 总期数
		createDto.setTotalPeriods(
				orderSchedule == null ? orderExclusivel.getTransportTime() : orderSchedule.getTotalPeriods());
		// 下单门店
		createDto.setStoreId(orderExclusivel.getPlaceStoreId());
		// 用户id
		createDto.setUserId(orderExclusivel.getUserId());
		// 用车时长
		createDto.setTransportTime(orderExclusivel.getTransportTime());
		// 所属分子公司
		createDto.setSubcompanyId(dealerDetail.getSubcompanyId());

		return createDto;
	}

	public static List<InvoiceCreateDto> toInvoiceCreateDto(NotifyBizDto notify,
			List<OrderScheduleDto> orderScheduleDtos, OrderExclusivelDto orderExclusivel,
			DealerDetailDto dealerDetail) {
		List<InvoiceCreateDto> createDtos = new ArrayList<>();

		// 服务费
		BigDecimal serviceFee = orderExclusivel.getServiceFee()
				.divide(new BigDecimal(orderScheduleDtos.get(0).getTotalPeriods()), 2, RoundingMode.HALF_DOWN);
		BigDecimal tmpServiceFee = BigDecimal.ZERO;
		for (OrderScheduleDto orderScheduleDto : orderScheduleDtos) {
			InvoiceCreateDto createDto = new InvoiceCreateDto();
			// 订单号
			createDto.setOrderCode(orderExclusivel.getOrderCode());
			// 下单时间
			createDto.setOrderTime(orderExclusivel.getOrderTime());
			// 支付时间/续订单生效的时
			createDto.setPayTime(notify.getPayTime());
			// 支付金额
			createDto.setAmount(orderScheduleDto.getPayable());
			// 服务费
			tmpServiceFee = tmpServiceFee.add(serviceFee);
			createDto.setServiceFee(orderScheduleDto.getPeriods() == orderScheduleDto.getTotalPeriods()
					? orderExclusivel.getServiceFee().subtract(tmpServiceFee) : serviceFee);
			// 业务类型（1：订阅里程，2：专属里程）
			createDto.setBusinessType(InvoiceBusinessTypeEnum.EXCLUSIVEL.getValue());
			// 发票类型（11：主订单，12：续订单，13：加购，14：超时，15：超程，16：质损,21：分期，22：全款）
			createDto.setType(InvoiceTypeEnum.FULL_PAYMENT.getValue());
			// 当前期数
			createDto.setCurrentPeriods(orderScheduleDto.getPeriods());
			// 总期数
			createDto.setTotalPeriods(orderScheduleDto.getTotalPeriods());
			// 下单门店
			createDto.setStoreId(orderExclusivel.getPlaceStoreId());
			// 用户id
			createDto.setUserId(orderExclusivel.getUserId());
			// 用车时长
			createDto.setTransportTime(orderExclusivel.getTransportTime());
			// 所属分子公司
			createDto.setSubcompanyId(dealerDetail.getSubcompanyId());

			createDtos.add(createDto);
		}

		return createDtos;
	}

	public static InvoiceCreateDto toInvoiceCreateDto(NotifyBizDto notify, OrderSubscribeDto orderSubscribe,
			DealerDetailDto dealerDetail) {
		InvoiceCreateDto createDto = new InvoiceCreateDto();
		// 订单号
		createDto.setOrderCode(orderSubscribe.getOrderCode());
		// 下单时间
		createDto.setOrderTime(orderSubscribe.getOrderTime());
		// 支付时间/续订单生效的时
		createDto.setPayTime(orderSubscribe.getParentOrderCode() == null ? notify.getPayTime()
				: DateUtil.getDayStart(orderSubscribe.getLeaseInception()));
		// 支付金额
		createDto.setAmount(orderSubscribe.getCartFee());
		// 服务费(金额乘以费率)
		createDto.setServiceFee(orderSubscribe.getCartFee()
				.multiply(orderSubscribe.getServiceFeeRate().divide(new BigDecimal(100), 2, RoundingMode.HALF_DOWN)));
		// 业务类型（1：订阅里程，2：专属里程）
		createDto.setBusinessType(InvoiceBusinessTypeEnum.SUBSCRIBE.getValue());
		// 发票类型（11：主订单，12：续订单，13：加购，14：超时，15：超程，16：质损,21：分期，22：全款）
		createDto.setType(orderSubscribe.getParentOrderCode() == null ? InvoiceTypeEnum.MAIN_ORDER.getValue()
				: InvoiceTypeEnum.REPEAT_ORDER.getValue());
		// 下单门店
		createDto.setStoreId(orderSubscribe.getPlaceStoreId());
		// 用户id
		createDto.setUserId(orderSubscribe.getUserId());
		// 用车时长
		createDto.setTransportTime(orderSubscribe.getTransportTime());
		// 所属分子公司
		createDto.setSubcompanyId(dealerDetail.getSubcompanyId());

		return createDto;
	}

	public static InvoiceCreateDto toInvoiceCreateDto(NotifyBizDto notify, OrderSubscribeDto orderSubscribe,
			DealerDetailDto dealerDetail, BigDecimal amount, InvoiceTypeEnum invoiceType) {
		InvoiceCreateDto createDto = new InvoiceCreateDto();
		// 订单号
		createDto.setOrderCode(orderSubscribe.getOrderCode());
		// 下单时间
		createDto.setOrderTime(orderSubscribe.getOrderTime());
		// 支付时间/续订单生效的时
		createDto.setPayTime(notify.getPayTime());
		// 支付金额
		createDto.setAmount(amount);
		// 服务费
		createDto.setServiceFee(BigDecimal.ZERO);
		// 业务类型（1：订阅里程，2：专属里程）
		createDto.setBusinessType(InvoiceBusinessTypeEnum.SUBSCRIBE.getValue());
		// 发票类型（11：主订单，12：续订单，13：加购，14：超时，15：超程，16：质损,21：分期，22：全款）
		createDto.setType(invoiceType.getValue());
		// 下单门店
		createDto.setStoreId(orderSubscribe.getPlaceStoreId());
		// 用户id
		createDto.setUserId(orderSubscribe.getUserId());
		// 用车时长
		createDto.setTransportTime(orderSubscribe.getTransportTime());
		// 所属分子公司
		createDto.setSubcompanyId(dealerDetail.getSubcompanyId());

		return createDto;
	}

	public static UploadFileRequestDto toUploadFileRequestDto(FileStorageConfigDto beanDto) {
		UploadFileRequestDto beanVo = null;
		if (beanDto != null) {
			beanVo = new UploadFileRequestDto();
			BeanUtils.copyProperties(beanDto, beanVo);
		}
		return beanVo;
	}

	public static ContractRenegeBizCreateDto toContractRenegeBizCreateDto(ContractRenegeBizCreateDto createDto,
	                                                                      OrderRenegeDto beanDto) {
		if (beanDto == null) {
			return createDto;
		}
		createDto = createDto == null ? new ContractRenegeBizCreateDto() : createDto;

		if (OrderRenegeCostTypeEnum.INCOME.equals(beanDto.getCostType())) {
			createDto.setCostType("2.1");
			createDto.setIncom(beanDto.getCostAmount().toPlainString());
			createDto.setIncomUpper(NumberToCNUtils.number2CNMontrayUnit(beanDto.getCostAmount()));
			createDto.setDisburse("/");
			createDto.setDisburseUpper("/");
		} else if (OrderRenegeCostTypeEnum.DISBURSE.equals(beanDto.getCostType())) {
			createDto.setCostType("2.2");
			createDto.setIncom("/");
			createDto.setIncomUpper("/");
			createDto.setDisburse(beanDto.getCostAmount().toPlainString());
			createDto.setDisburseUpper(NumberToCNUtils.number2CNMontrayUnit(beanDto.getCostAmount()));
		}
		return createDto;
	}

	public static ContractRenegeBizCreateDto toContractRenegeBizCreateDto(ContractRenegeBizCreateDto createDto,
	                                                                      UserBizDto beanDto) {
		if (beanDto == null) {
			return createDto;
		}
		createDto = createDto == null ? new ContractRenegeBizCreateDto() : createDto;

		createDto.setPartyBId(beanDto.getId());
		createDto.setPartyBName(beanDto.getRealName());
		createDto.setPartyBCard(beanDto.getIdNumber());
		createDto.setPartyBAddress(beanDto.getAddress());
		createDto.setPartyBPhone(beanDto.getPhone());

		return createDto;
	}

	public static ContractRenegeBizCreateDto toContractRenegeBizCreateDto(ContractRenegeBizCreateDto createDto,
	                                                                      SubcompanyDto beanDto) {
		if (beanDto == null) {
			return createDto;
		}
		createDto = createDto == null ? new ContractRenegeBizCreateDto() : createDto;

		createDto.setPartyAId(beanDto.getId());
		createDto.setPartyAName(beanDto.getFullName());
		if ("2.1".equals(createDto.getCostType())) {
			createDto.setPartyA(beanDto.getFullName());
			createDto.setPartyABankAccount(beanDto.getBankNumber());
			createDto.setPartyABank(beanDto.getAccountBank());
		} else {
			createDto.setPartyA("/");
			createDto.setPartyABankAccount("/");
			createDto.setPartyABank("/");
		}

		return createDto;
	}


}
